package com.android.notepad2;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public class MyNotePad2 extends ListActivity {
	
	private NotesDbAdapter theadapter;
	private Cursor thecursor;
	
	private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.layout_list);
    	theadapter = new NotesDbAdapter(this);
    	theadapter.open();
    	fillData();
    	registerForContextMenu(getListView());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	boolean result = super.onCreateOptionsMenu(menu);
    	menu.add(0, INSERT_ID, 0, R.string.menu_insert);
    	return result;
    }

   public boolean onMenuItemSelected(int featureId, MenuItem item) {
	   switch (item.getItemId()) {
	   case INSERT_ID:
	   createNote();
	   return true;
	   }
	   return super.onMenuItemSelected(featureId,item);
   }
   
   @Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DELETE_ID, 0, R.string.menu_delete);
	}

   @Override
	public boolean onContextItemSelected(MenuItem item) {
		switch(item.getItemId()) {	
			case DELETE_ID:
				AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
				theadapter.deleteNote(info.id);
				fillData();
				return true;
		}
		return super.onContextItemSelected(item);
	}
   
   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
       super.onListItemClick(l, v, position, id);

       // TODO: fill in rest of method
       
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
       super.onActivityResult(requestCode, resultCode, intent);
       
       // TODO: fill in rest of method
       
   }  

   private void createNote() {
	   theadapter.createNote("Some Note", "");
	   fillData();
   }
   
   
   private void fillData() {
       // Get all of the rows from the database and create the item list
       thecursor = theadapter.fetchAllNotes();
       startManagingCursor(thecursor);
       
       // Create an array to specify the fields we want to display in the list (only TITLE)
       String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
       
       // and an array of the fields we want to bind those fields to (in this case just text1)
       int[] to = new int[]{R.id.the_text};
       
       // Now create a simple cursor adapter and set it to display
       SimpleCursorAdapter notes = 
       	    new SimpleCursorAdapter(this, R.layout.layout_row, thecursor, from, to);
       setListAdapter(notes);
   }
}
